2021.12.9 java代码对接sap接口(soap协议、webservice)

您所在的位置:网站首页 sap 接口开发 2021.12.9 java代码对接sap接口(soap协议、webservice)

2021.12.9 java代码对接sap接口(soap协议、webservice)

2023-09-05 12:45| 来源: 网络整理| 查看: 265

2021.12.9 java对接sap接口(soap协议、webservice) 问题:对接sap接口,代码调试 执行: 1、soapui 软件测试是否能正确访问

未能正确访问,因为未在本地配置域名映射

要在C:\Windows\System32\drivers\etc里面配置。

# localhost name resolution is handled within DNS itself. 127.0.0.1 localhost # ::1 localhost 192.168.2.21 xxx.xxx.com

配置完后soapui软件访问成功!

2、代码测试 2.1、用axis框架的方式测试连接 package com.ly.utils; import org.apache.axis.client.Call; import org.apache.axis.client.Service; import org.apache.axis.encoding.XMLType; import org.apache.axis.message.SOAPHeaderElement; import javax.xml.rpc.ParameterMode; import java.util.List; public class SendWebServiceUtil { /** *axis 方式 * @param nameSpace 命名空间 * @param endpoint wsdl发布地址 * @param methods 方法名 * @param list 参数列表 * @return 返回值 * @author LY */ public String webserviceConn(String userName,String passWord, String nameSpace, String endpoint, String methods, List list) { String result = null; Object[] parameter = new Object[list.size()]; try { Service service = new Service(); Call call = (Call) service.createCall(); call.setTargetEndpointAddress(new java.net.URL(endpoint)); call.setOperationName(new javax.xml.namespace.QName(nameSpace,methods));//WSDL里面描述的接口名称 for(int i=0;i System.out.println(e.getMessage()); } return result; } } /* * org.apache.axis axis 1.4 * javax.xml jaxrpc-api 1.1 * * */ 2.2、用axis2方式的访问 package com.ly.webservice; import org.apache.axis2.AxisFault; import org.apache.axis2.addressing.EndpointReference; import org.apache.axis2.client.Options; import org.apache.axis2.rpc.client.RPCServiceClient; import org.apache.axis2.transport.http.HTTPConstants; import org.apache.axis2.transport.http.HttpTransportProperties; import javax.xml.namespace.QName; public class Axis2TestConn { public static void main(String[] args) { String[]arrs={"WMS"}; // try { // testConn("http://sappijid.informrack.com:50000/XISOAPAdapter/MessageServlet?senderParty=&senderService=BC_WMS&receiverParty=&receiverService=&interface=SI_IFS007_SYNC_OUT_500&interfaceNamespace=http://informrack.com/ERP/MM", // "urn:sap-com:document:sap:rfc:functions", // "ZYYF01_IFS007",arrs, // "piappljid", // "yf123456"); // } catch (AxisFault axisFault) { // axisFault.printStackTrace(); // } try { testConn("http://localhost:8088/ws-test/hello?wsdl", "http://service.ly.com/", "sayHello",arrs, "piappljid", "yf123456"); } catch (AxisFault axisFault) { axisFault.printStackTrace(); } } public static void testConn(String serviceName, String namespace, String methodName, Object[]methodArgs, String username, String password) throws AxisFault { Class[] returnTypes = new Class[] { String.class };//返回值类型 //RPC方式调用webservice RPCServiceClient serviceClient = new RPCServiceClient(); Options options = serviceClient.getOptions(); HttpTransportProperties.Authenticator basicauth = new HttpTransportProperties.Authenticator(); basicauth.setUsername(username); //服务器访问用户名 basicauth.setPassword(password); //服务器访问密码 options.setProperty(HTTPConstants.AUTHENTICATE, basicauth); //指定调用的url EndpointReference targetEPR = new EndpointReference(serviceName); options.setTo(targetEPR); //参数:命名空间、方法名 QName op= new QName(namespace,methodName); //调用远程f方法,并指定方法参数以及返回值类型 Object[] response = serviceClient.invokeBlocking(op, methodArgs,returnTypes); for (Object o : response) { System.out.println(o.toString()); } //System.out.println(result); } } 2.3、http方式,直接发送请求报文 import java.io.InputStream; import org.apache.commons.httpclient.HttpClient; import org.apache.commons.httpclient.UsernamePasswordCredentials; import org.apache.commons.httpclient.auth.AuthScope; import org.apache.commons.httpclienthods.PostMethod; import org.apache.commons.httpclienthods.RequestEntity; import org.apache.commons.httpclienthods.StringRequestEntity; import org.apache.commons.io.IOUtils; public class HttpConnWebServiceUtil { public static void main(String[] args) throws Exception { httpConnSap( "你需要访问的目标URL", "用户名","密码" ); } /**http 方式直接连接sap * @param endPointAddress webservice发布地址 * @param username 用户名 * @param password 密码 * @Author LY */ public static void httpConnSap(String endPointAddress,String username,String password) throws Exception { // HttpClient发送SOAP请求 int timeout = 10000; System.out.println("HttpClient 发送SOAP请求"); HttpClient client = new HttpClient(); //如果需要用户名密码验证;不需要验证登录则不需要以下4行 UsernamePasswordCredentials creds = new UsernamePasswordCredentials(username, password); client.getState().setCredentials(AuthScope.ANY, creds); //webservice 地址 PostMethod postMethod = new PostMethod(endPointAddress); // 设置连接超时 client.getHttpConnectionManager().getParams().setConnectionTimeout(timeout); // 设置读取时间超时 client.getHttpConnectionManager().getParams().setSoTimeout(timeout); // 然后把Soap请求数据添加到PostMethod中 RequestEntity requestEntity = new StringRequestEntity(getXML(),"text/xml", "UTF-8"); // 设置请求头部,否则可能会报 “no SOAPAction header” 的错误 postMethod.setRequestHeader("SOAPAction", ""); // 设置请求体 postMethod.setRequestEntity(requestEntity); int status = client.executeMethod(postMethod); if (status == 200) {// 成功 InputStream is = postMethod.getResponseBodyAsStream(); // 获取请求结果字符串 String result = IOUtils.toString(is); System.out.println("请求成功!"+"\n"+"返回结果:"+result); } else { System.out.println("请求失败!"+"\n"+"错误代码:"+status+"\n"+"返回报文:"+"\n"+postMethod.getResponseBodyAsString()); } } /** * @return 获取请求报文 这里我是直接从soapui 里面测试再把请求报文复制过来的。 */ public static String getXML(){ String soapXML="\n" + " \n" + " \n" + " \n" + " \n" + "\n" + "\n" + "WMS\n" + "\n" + "ZYYF01_IFS007\n" + "\n" + "\n" + "\n" + "\n" + "\n" + "\n" + "000100118369\n" + "\n" + "RP21112415372546\n" + "\n" + "0012\n" + "\n" + "100.0\n" + "\n" + "2021-11-24\n" + "\n" + "1669.9\n" + "\n" + "KG\n" + "\n" + "1669.9\n" + "\n" + "KG\n" + "\n" + "1669.9\n" + "\n" + "KG\n" + "\n" + "1669.9\n" + "\n" + "KG\n" + "\n" + "1669.9\n" + "\n" + "229A1\n" + "\n" + "EA\n" + "\n" + "\n" + "\n" + "\n" + " \n" + " \n" + " \n" + ""; return soapXML; } } 最后,经过测试还是选择了这种方式。 3、问题遗留

2.1和2.2的方式,我访问本地发布的webservice没有问题,但是访问别人的就有问题,求解答。2.3的方式都可以访问

可能原因,就是请求报文的问题

在这里插入图片描述

测试了一下,是请求报文的问题,访问网址自动生成的报文去请求 报500错误,要使用特定的报文,所以还是要拼接报文,2,.1和2.2如何拼接报文?(是否有必要)

注释一些报文后就请求成功了。

在这里插入图片描述

4、相关工具

soapui(接口测试工具)下载地址(一直滑到最下面找到普通下载,不然选择其他的会给你安装一些流氓软件)

https://www.zdfans.com/html/18711.html



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3